package com.shujia

import scala.io.Source

object Code32JoinComputer {
  def main(args: Array[String]): Unit = {
    val stuInfoList: List[(String, String, Int, String, String)] = Source
      .fromFile("scala_code/data/students.txt")
      .getLines()
      // 将读取到的每一行数据转换成List中的元素
      .toList
      .map(
        (line: String) => {
          val colum: Array[String] = line.split(",")
          // (id,name,age,gender,clazz)
          //  Tuple相比较Array 其元素的数据类型可以多样
          (colum(0), colum(1), colum(2).toInt, colum(3), colum(4))
        }
      )

    val scoreList: List[(String, String, String)] = Source
      .fromFile("scala_code/data/score.txt")
      .getLines()
      // 将读取到的每一行数据转换成List中的元素
      .toList
      .map(
        line => {
          val column: Array[String] = line.split(",")
          (column(0), column(1), column(2))
        }
      )

    val scoreMapKeyValue: Map[(String, String), String] = scoreList
      .map(
        // 将一个Tuple3 转成Tuple2 其中Value是另外一个Tuple
        oneScore => ((oneScore._1, oneScore._2), oneScore._3)
      ).toMap

    // 方式1：
    stuInfoList
      .map(
        oneInfo => {
         val infoId: String = oneInfo._1

          val joinScoreRes: Map[(String, String), String] = scoreMapKeyValue.filter(
            scoreKeyValue => {
              val scoreID: String = scoreKeyValue._1._1
              scoreID == infoId
            }
          )
          (oneInfo,joinScoreRes)
        }
      )
      // 需求：将如下结果进行转换
      //  ((1500100999,钟绮晴,23,女,文科五班),Map((1500100999,1000005) -> 41, (1500100999,1000006) -> 10, (1500100999,1000004) -> 48, (1500100999,1000002) -> 55, (1500100999,1000001) -> 115, (1500100999,1000003) -> 102))
      //  (1500100999,钟绮晴,23,女,文科五班,1000005,41)
      //  (1500100999,钟绮晴,23,女,文科五班,1000006,10) ...

      // flatMap 读取 一个元素 数据 返回一个集合 将集合中的所有数据提取到结果集合中
      .flatMap(
        x => {
          val stuInfo: (String, String, Int, String, String) = x._1
          // 一个学生对应所有科目的成绩数据
          val oneStuAllScore: Map[(String, String), String] = x._2
          // 取出每个学生每个科目成绩 和学生基本信息进行拼接
          oneStuAllScore.map(
            oneScore => (stuInfo._1,stuInfo._2,stuInfo._3,stuInfo._4,stuInfo._5,oneScore._1._2,oneScore._2)
          )
        }
      )
//      .foreach(println)
      .foreach(println(_))





  }
}
